package widget.book.viewer
{
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import widget.common.utils.MathUtils;
	import widget.book.manager.BookManager;
	
	public class ShadowViewer extends Sprite{
		private var fillType:String = GradientType.LINEAR;
		private var spreadMethod:String = SpreadMethod.PAD;
		private var colors:Array = null;
		private var colors1:Array = null;
		private var alphas1:Array = null;
		private var alphas2:Array = null;
		private var ratios:Array = null;
		private var matr:Matrix = null;
		
		private var myscale:Number = 0;
		private var myalpha:Number = 0;
		
		private var shadow1:Shape = null;
		private var shadow2:Shape = null;
		private var mask1:Shape = null;
		private var mask2:Shape = null;
		private var w:int = 0;
		private var h:int = 0;
		public function ShadowViewer()
		{
			super();
			init();
		}
		
		private function init():void {
			w = BookManager.getInstance().bookModel.width/2;
			h = BookManager.getInstance().bookModel.height;
			
			colors = [0x0, 0x0];
			colors1 = [0xff0000,0x00ff00];
			alphas1 = [0,1];
			alphas2 = [1,0];
			ratios = [0, 255];
			
			shadow1 = new Shape();
			shadow2 = new Shape();
			mask1 = new Shape();
			mask2 = new Shape();
			
			drawRect(mask1, w, h);
			drawRect(mask2, w * 2, h);
			drawShadow();
			
			addChild(mask1);
			addChild(shadow1);
			shadow1.mask = mask1;
			shadow1.x = 2 * w;
			
			addChild(mask2);
			addChild(shadow2);
			shadow2.mask = mask2;
			shadow2.x = 2 * w;
			
			matr = new Matrix();
		}
		
		private function drawRect(sh:Shape,width:uint,height:uint):void {
			sh.graphics.beginFill(0, 1);
			sh.graphics.drawRect(0, 0, width, height);
			sh.graphics.endFill();
		}
		
		private function drawShadow():void {
			matr.createGradientBox(w, h, 0,-w/2+50, 0);
			shadow1.graphics.beginGradientFill(fillType, colors, alphas1, ratios, matr, spreadMethod);
			shadow1.graphics.drawRect(0, -h*1.5 , 50, h *3);
			shadow1.graphics.beginGradientFill(fillType, colors, alphas2, ratios, matr, spreadMethod);
			shadow1.graphics.drawRect(50, -h*1.5, w, h * 3);
			shadow1.graphics.endFill();
			
			shadow2.graphics.beginGradientFill(fillType, colors, alphas2, ratios, matr, spreadMethod);
			shadow2.graphics.drawRect(0, -h * 2, w, h * 4);
			shadow2.graphics.endFill();
		}
		
		public function setUp(mousePoint:Point, maskPoint:Point, r:Number, scale:Number, area:Number):void {
			var ratio = MathUtils.distance(mousePoint.x, mousePoint.y, area % 2 == 0?0:w*2, area < 2 ?0:h);
			ratio = ratio / w/2;
			mask1.x = mousePoint.x;
			mask1.y = mousePoint.y;
			mask1.scaleX = area < 2? -scale: scale;
			mask1.rotation = area < 2? r * 2:r * 2 - 180;
			
			shadow1.x = maskPoint.x;
			shadow1.y = maskPoint.y;
			shadow1.scaleX = scale * (ratio<0.1?0.1:ratio);
			shadow1.alpha = 1 - ratio;
			shadow1.rotation = r;
			
			shadow2.x = maskPoint.x;
			shadow2.y = maskPoint.y;
			shadow2.scaleX = -scale;
			shadow2.rotation = r;
			shadow2.alpha = 1 - ratio;
		}
	}
}